//
//  LXEQViewController.m
//  bgms_d
//
//  Created by lachesis-zhu on 2019/1/18.
//  Copyright © 2019 lachesis-zhu. All rights reserved.
//

#import "LXEQViewController.h"
#import "UIView+DrawSomething.h"

@interface LXEQViewController ()
@property (nonatomic, strong) IBOutlet UIButton *clubBtn;
@property (nonatomic, strong) IBOutlet UIButton *rockBtn;
@property (nonatomic, strong) IBOutlet UIButton *volBtn;
@property (nonatomic, strong) IBOutlet UIButton *popBtn;
@property (nonatomic, strong) IBOutlet UIButton *flatBtn;

@property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *btnArrays;


@end

@implementation LXEQViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.clubBtn setBackgroundImage:[UIImage imageWithColor:kAppGrayBgColor] forState:UIControlStateSelected];
    [self.popBtn setBackgroundImage:[UIImage imageWithColor:kAppGrayBgColor] forState:UIControlStateSelected];
    [self.flatBtn setBackgroundImage:[UIImage imageWithColor:kAppGrayBgColor] forState:UIControlStateSelected];
    [self.volBtn setBackgroundImage:[UIImage imageWithColor:kAppGrayBgColor] forState:UIControlStateSelected];
    [self.rockBtn setBackgroundImage:[UIImage imageWithColor:kAppGrayBgColor] forState:UIControlStateSelected];
    
    [self addNotification];
    [self refetchEQ];
}

- (void)refetchEQ {
    UInt8 data[3];
    data[0] = 0x02;
    data[1] = 0x00;
    data[2] = 0x00;
    [[BLETransDeviceSingleton sharedInstance] characteristc:EQ_CHARACTERISTIC_UUID sendHostCmd:data lenght:3];
}

- (void)resetEQ:(NSInteger)index
{
    UInt8 data[3];
    data[0] = 0x01;
    data[1] = 0xff & index;
    data[2] = 0x00;
    [[BLETransDeviceSingleton sharedInstance] characteristc:EQ_CHARACTERISTIC_UUID sendHostCmd:data lenght:3];
}

- (IBAction)buttonDown:(UIButton *)sender {
    [self.btnArrays enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIButton *button = obj;
        button.selected = NO;
    }];
    sender.selected = YES;
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [self resetEQ:sender.tag];
}

#pragma mark -  notification
- (void)addNotification
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(volNotication:) name:EQ_CHARACTERISTIC_UUID object:nil];
}

- (void)volNotication:(NSNotification *)notification
{
    NSData *data = notification.userInfo[kAppHeadUnitData];
    Byte *MessageBt = (Byte *)[data bytes];
    NSInteger index = MessageBt[2];
    if (self.btnArrays.count > index - 1) {
        UIButton *btn = [self.btnArrays objectAtIndex:index - 1];
        btn.selected = YES;
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
